package com.szgr.baoyin.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.szgr.baoyin.R;
import com.szgr.baoyin.model.ForumDetailItem;
import com.szgr.baoyin.myview.CircleImageView;
import com.szgr.baoyin.utils.MyTool;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Jimbo on 2015/10/11.
 */
public class ForumDetailListAdapter extends BaseAdapter {
    private Context mContext;
    private List<ForumDetailItem> data;
    public ForumDetailListAdapter(Context context,List<ForumDetailItem> data){
        mContext = context;
        this.data = data;
    }
    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_forum_detail_list_view,parent,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();

        ForumDetailItem forumDetailItem = data.get(position);
        MyTool.showPicture(mContext, holder.civ_head, forumDetailItem.getIcon());
        holder.tv_name.setText(forumDetailItem.getReply_name());
        holder.tv_title.setVisibility(View.GONE);
        holder.tv_time.setText(MyTool.getStandardDate(forumDetailItem.getPosts_date()));
        try {
            List<ForumDetailItem.ReplyContent> reply_content = (List<ForumDetailItem.ReplyContent>) forumDetailItem.getReply_content();
            ForumDetailItem.ReplyContent replyContent = reply_content.get(0);
            holder.tv_content.setText(replyContent.getInfor());
        } catch (Exception e) {
            e.printStackTrace();
        }


        return convertView;
    }

    public void setData(List<ForumDetailItem> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    private class ViewHolder {
        CircleImageView civ_head;
        TextView tv_name;
        TextView tv_time;
        TextView tv_title;
        TextView tv_content;

        public ViewHolder(View view){
            civ_head = (CircleImageView) view.findViewById(R.id.item_forum_head_view);
            tv_name = (TextView) view.findViewById(R.id.item_forum_name);
            tv_time = (TextView) view.findViewById(R.id.item_forum_time);
            tv_title = (TextView) view.findViewById(R.id.item_forum_title);
            tv_content = (TextView) view.findViewById(R.id.item_forum_content);
        }
    }
}
